<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.Topic' );
import( 'classes.model.Case' );
import( 'classes.logic.Authentication' );
//import( 'de.ceus-media.framework.krypton.core.database.StatementBuilder' );
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
class Logic_Case extends Framework_Krypton_Core_Logic
{
	function addCase( $data, $prefix = "" )
	{
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'management/structure', 'addCase', $data, $prefix );
		$topic	= new Model_Topic( $data['topic_id'] );
		if( !$topic->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_topic_id' );
		$data['author_id']	= Logic_Authentication::getCurrentUser();
		$data['created']	= time();
		$case	= new Model_Case();
		return $case->insert( $data, false );
	}
	
	function editCase( $caseId, $data, $prefix = "" )
	{
		if( !$caseId )
		 	throw new Framework_Krypton_Exception_Logic( 'case_id_missing' );
		$case	= new Model_Case( $caseId );
		if( !$case->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_case_id' );
		$data	= $this->removePrefixFromFields( $data, $prefix );
		print_m( $data );
		$this->validateForm( 'management/structure', 'editCase', $data, $prefix );
		$data['author_id']	= Logic_Authentication::getCurrentUser();
		$data['modified']	= time();
		return $case->update( $data, false );
	}

	static function getCaseList( $topicId, $filters = array(), $orders = array() )
	{
		$model	= new Model_Case();
		$conditions	= array_merge( array( 'topic_id' => $topicId ), $filters );
		return $model->find( array(), $conditions, $orders );
	}

	static function getCaseData( $caseId )
	{
		$model	= new Model_Case( $caseId );
		$data	= $model->get();
		if( !$data )
			throw new Framework_Krypton_Exception_Logic( 'invalid_case_id' );
		return $data;
	}
	
	static function getCaseDescription( $caseId )
	{
		$data	= self::getCaseData( $caseId );
		return $data['description'];
	}

	static function getCaseTitle( $caseId )
	{
		$data	= self::getCaseData( $caseId );
		return $data['title'];
	}
}
?>
